import axios from 'axios'
import {getCookie} from './cookie'

// create an axios instance
const service = axios.create({
  baseURL: import.meta.env.VITE_BASE_API, 
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})

// request拦截器
service.interceptors.request.use(config => {
  
  let token = getCookie('token')
  if (token) {
		//config.headers['Authorization'] = token 
    config.headers['token'] = token
	}
  return config
}, error => {
  Promise.reject(error)
})

// 响应拦截
service.interceptors.response.use(res => {
  if(res.data.code == -1){
    
  }
  return res
}, error => {
  if(error.response.status == 500){
    Toast(error.response.data.message)
    return Promise.resolve(error.response)
  }else{
    return Promise.resolve(error.response)
  }
})

export default service

